/*
 * Powered By newdev
 * Since 2020
 */

package com.xy.biz.uc.client;

import com.xy.biz.uc.domain.dto.UserGroupDTO;
import com.xy.biz.uc.domain.req.GetUserGroupByRequest;
import com.xy.biz.uc.domain.req.GetUserGroupRequest;
import com.xy.biz.uc.domain.req.ListUserGroupRequest;
import com.xy.biz.uc.domain.resp.GetUserGroupResponse;
import com.xy.lang.domain.Result;
import com.xy.lang.util.ResultUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 *
 * @author wwj
 * @date 2021-4-7
 */
public interface UserGroupClient {


	/**
	 *
	 * @param
	 * @return
	 */
	Result<List<UserGroupDTO>> listUserGroupByIds(Collection<Long> collection);

	Result<List<UserGroupDTO>> listUserGroup(ListUserGroupRequest request);
	Result<UserGroupDTO> getUserGroup(GetUserGroupRequest request);

	default <T> void buildByUserGroup(Collection<T> source,
                                 Collection<Long> userGroupIdCollection,
                                 Function<T, Long> userGroupIdGetter,
                                 BiConsumer<T, UserGroupDTO> builder) {
		Objects.requireNonNull(userGroupIdGetter, "The userIdGetter is required.");
		Objects.requireNonNull(builder, "The builder is required.");
		if(source == null || source.size() == 0) {
			return;
		}
		if(userGroupIdCollection == null || userGroupIdCollection.size() == 0) {
			return;
		}
		Set<Long> userGroupIdSet = new HashSet<>(userGroupIdCollection);
		List<UserGroupDTO> userGroupDTOList = ResultUtils.get(listUserGroupByIds(userGroupIdSet));
		if(userGroupDTOList != null && userGroupDTOList.size() > 0) {
			Map<Long, UserGroupDTO> userGroupDTOMap = userGroupDTOList.stream().collect(Collectors.toMap(UserGroupDTO::getUserGroupId, e -> e));
			UserGroupDTO userGroupDTO;
			Long userGroupId;
			for(T entity : source) {
				userGroupId = userGroupIdGetter.apply(entity);
				userGroupDTO = userGroupDTOMap.get(userGroupId);
				if(userGroupDTO != null) {
					builder.accept(entity, userGroupDTO);
				}
			}
		}
	}


	Result<List<GetUserGroupResponse>> listUserGroup(GetUserGroupByRequest request);

	Result<List<GetUserGroupResponse>> listMyUserGroup(GetUserGroupByRequest request);

}
